home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / !SFcolours / c / SFCSaveBox < prev    next >
Encoding:
Text File  |  2003-10-24  |  5.9 KB  |  173 lines

  1. /*
  2.  *  SFcolours - Star Fighter 3000 colours editor
  3.  *  Colours file savebox
  4.  *  Copyright (C) 2001  Chris Bazley
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public Licence as published by
  8.  *  the Free Software Foundation; either version 2 of the Licence, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public Licence for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public Licence
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /* ANSI library files */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdbool.h>
  26.  
  27. /* RISC OS library files */
  28. #include "wimp.h"
  29. #include "toolbox.h"
  30. #include "event.h"
  31. #include "wimplib.h"
  32. #include "saveas.h"
  33. #include "dcs.h"
  34.  
  35. /* My library files */
  36. #include "err.h"
  37. #include "msgtrans.h"
  38. #include "Macros.h"
  39. #include "SFformats.h"
  40. #include "Loader.h"
  41. #include "FilePerc.h"
  42.  
  43. /* Local headers */
  44. #include "EditColmap.h"
  45. #include "SFCSaveBox.h"
  46. #include "utils.h"
  47. #include "DCS_dialogue.h"
  48.  
  49. ObjectId savebox_sharedid = NULL_ObjectId;
  50.  
  51. /* ----------------------------------------------------------------------- */
  52. /*                       Function prototypes                               */
  53.  
  54. static ToolboxEventHandler _SaveFile_savecompleted, _SaveFile_savehandler, _SaveFile_abouttoopen, _SaveFile_buttonshandler;
  55. static void set_filename(ViewData *view_data);
  56.  
  57. /* ----------------------------------------------------------------------- */
  58. /*                         Public functions                                */
  59.  
  60. void SaveFile_initialise(IdBlock *id_block)
  61. {
  62.   savebox_sharedid = id_block->self_id;
  63.  
  64.   ObjectId savebox_windowid;
  65.   EF(saveas_get_window_id(0, savebox_sharedid, &savebox_windowid));
  66.   EF(event_register_toolbox_handler(savebox_windowid, ActionButton_Selected, _SaveFile_buttonshandler, NULL));
  67.  
  68.   EF(event_register_toolbox_handler(savebox_sharedid, SaveAs_AboutToBeShown, _SaveFile_abouttoopen, NULL));
  69.   EF(event_register_toolbox_handler(savebox_sharedid, SaveAs_SaveCompleted, _SaveFile_savecompleted, NULL));
  70.   EF(event_register_toolbox_handler(savebox_sharedid, SaveAs_SaveToFile, _SaveFile_savehandler, NULL));
  71. }
  72.  
  73. /* ----------------------------------------------------------------------- */
  74. /*                         Private functions                               */
  75.  
  76. static int _SaveFile_buttonshandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  77. {
  78.   ActionButtonSelectedEvent *abse = (ActionButtonSelectedEvent *)event;
  79.   if(id_block->self_component != 0x82bc02 || !FLAG_SET(abse->hdr.flags, ActionButton_Selected_Adjust))
  80.     return 0; /* not interested */
  81.  
  82.   /* Cancel button clicked on underlying window - reset dialogue box */
  83.   ViewData *view_data;
  84.   ObjectId sb_ancestor;
  85.   E_RETV(toolbox_get_ancestor(0, savebox_sharedid, &sb_ancestor, NULL), 1);  
  86.   if(!E(toolbox_get_client_handle(0, sb_ancestor, (void **)&view_data)))
  87.     set_filename(view_data);
  88.  
  89.   return 1; /* claim event */
  90. }
  91.  
  92. /* ----------------------------------------------------------------------- */
  93.  
  94. static int _SaveFile_abouttoopen(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  95. {
  96.   /* Set up dialogue box for ancestor document */
  97.   ViewData *view_data;
  98.   if(!E(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data)))
  99.     set_filename(view_data);
  100.  
  101.   return 0; /* pass this event on */
  102. }
  103.  
  104. /* ----------------------------------------------------------------------- */
  105.  
  106. static int _SaveFile_savecompleted(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  107. {
  108.   SaveAsSaveCompletedEvent *sasc = (SaveAsSaveCompletedEvent *)event;
  109.   ViewData *view_data;
  110.  
  111.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
  112.   
  113.   if(FLAG_SET(sasc->hdr.flags, SaveAs_DestinationSafe)) {
  114.     /* Mark data as saved */
  115.     char *buf;
  116.     if(!E(loader_canonicalise(&buf, NULL, NULL, sasc->filename))) {
  117.       EditColmap_newfile(view_data, buf, true);
  118.       free(buf);
  119.     } else
  120.       EditColmap_newfile(view_data, sasc->filename, true);
  121.  
  122.     /* If we were opened from the DCS dbox, close the document window */
  123.     if(id_block->parent_id == dcs_sharedid) {
  124.       if(dcs_openparent) /* (set if ADJUST-click on document's close icon) */
  125.         EditColmap_openparentdir(view_data); /* open parent directory of file */
  126.   
  127.       RE(toolbox_delete_object(0, id_block->ancestor_id))
  128.     }
  129.   }
  130.  
  131.   return 1; /* claim event */
  132. }
  133.  
  134. /* ----------------------------------------------------------------------- */
  135.  
  136. static int _SaveFile_savehandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  137. {
  138.   SaveAsSaveToFileEvent *sastf = (SaveAsSaveToFileEvent *)event;
  139.   ViewData *view_data;
  140.   _kernel_oserror *err;
  141.  
  142.   err = toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data);
  143.   if(err != NULL)
  144.     goto SaveFail;
  145.  
  146.   /* Save Fednet file */
  147.   err = perc_operation(FILEPERC_OP_COMP, sastf->filename, FILETYPE_FEDNET, (flex_ptr)&view_data->colour_map);
  148.   if(err != NULL)
  149.     goto SaveFail;
  150.  
  151.   RE(saveas_file_save_completed(1, id_block->self_id, sastf->filename))
  152.   return 1; /* claim event */
  153.  
  154.   SaveFail:
  155.     err_report(err->errnum, msgs_lookup_sub1("SaveFail", err->errmess));
  156.     RE(saveas_file_save_completed(0, id_block->self_id, sastf->filename))
  157.     return 1; /* claim event */
  158.   
  159. }
  160.  
  161. /* ----------------------------------------------------------------------- */
  162.  
  163. static void set_filename(ViewData *view_data)
  164. {
  165.   char *filename;
  166.   if(strcmp(view_data->last_savepath, "<untitled>") == 0)
  167.     filename = "Colours"; /* invent sensible leafname */
  168.   else
  169.     /* use existing title (whether leafname or full path) */
  170.     filename = view_data->last_savepath;
  171.   RE(saveas_set_file_name(0, savebox_sharedid, filename))
  172. }
  173.